package com.ruoyi.security.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @author Administrator
 * @version 1.0
 **/




@Configuration
//@EnableAutoConfiguration(exclude = {
//    org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration.class
//})
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true, jsr250Enabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    //认证管理器
    @Bean
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }
    //密码编码器
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Autowired
    private MyAuthenticationFailureHandler myAuthenticationFailureHandler;

    @Autowired
    private MyAuthenticationSuccessHandler myAuthenticationSuccessHandler;


    // 认证
    // There is no PasswordEncoder mapped for the id "null"
    // 密码没被加密会报错～
    // 在 spring security 5.0+ 中新增了很多密码加密到方法～
//    @Override
//    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//        auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
//            .withUser("jdlk")
//            .password(new BCryptPasswordEncoder().encode("admin123")).roles("ADMIN")
//            .and()
//            .withUser("123")
//            .password(new BCryptPasswordEncoder().encode("123")).roles("ADMIN");
//    }


    //安全拦截机制（最重要）
    @Override
    protected void configure(HttpSecurity http) throws Exception {


        http.csrf().disable()
            .formLogin() // 表单登录
//            .loginPage("/login.html") // 配置自定义登录页面地址
//            .loginProcessingUrl("/login") // 处理表单登录 URL 注意这里即使是使用默认的登录页面也一定要指定，否则资源服务器会找不到路径报404
//            .successHandler(myAuthenticationSuccessHandler)//登录成功后处理
            .defaultSuccessUrl("http://localhost/login")
            .failureHandler(myAuthenticationFailureHandler)//登录失败后处理
           .and()
            .exceptionHandling()
            .and()
            .authorizeRequests()// 授权配置
            .antMatchers("/oauth/**", "/fjw/**","/login","/login/**", "logout/**")
            .permitAll()//都放行
            .anyRequest() // 其它所有请求
            .authenticated() // 都需要认证
        ;

    }

    }
